_DATE = "June 13, 2014 00:00:00";
_TEXT = "rgba(255, 255, 255, 0.75)";
_SHADOW = "rgba(0, 0, 0, 0.25)";
_CAPTION = true;
_LATITUDE = 45.81187;
_LONGITUDE = 4.72014;

(function ($) {
	$.fn.spinload = function (opts) {
		var element = this;
		var spinner = new Spinner({
			"lines": 10,
			"length": 10,
			"width": 5,
			"radius": 20,
			"corners": 1,
			"rotate": 0,
			"direction": 1,
			"color": "#FFFFFF",
			"speed": 1,
			"trail": 50,
			"shadow": false,
			"hwaccel": false,
			"className": "spinner",
			"zIndex": 0,
			"top": "auto",
			"left": "auto"
		}).spin($(element).find(".spinload").get(0));
		
		$(window).bind("load", function (event) {
			setTimeout(function () {
				$(element).find(".spinload").remove();
			}, 1000);
		});
		return (this);
	}
})(jQuery);

(function ($) {
	$.fn.placeholder = function (opts) {
		var element = this;

		$(window).bind("load", function (event) {
			$("[placeholder]").each(function (index) {
				var selection = this;
				
				$(selection).val($(selection).val() == "" ? $(selection).attr("placeholder") : $(selection).val());
			});
			$("[placeholder]").bind("focus", function (event) {
				var selection = this;
				
				$(selection).val($(selection).val() == $(selection).attr("placeholder") ? "" : $(selection).val());
			});
			$("[placeholder]").bind("blur", function (event) {
				var selection = this;
				
				$(selection).val($(selection).val() == "" ? $(selection).attr("placeholder") : $(selection).val());
			});
		});
		return (this);
	}
})(jQuery);

(function ($) {
	$.fn.substitute = function (html) {
		var element = this;
		var status = false;
		
		$(element).find(".button").bind("click", function (event) {
			if ($(element).find(".data").find(".field").size() != 0) {
				$(element).find(".data").find(".field").each(function (index) {
					var selection = this;
				
					$(selection).attr("class", $(selection).val() == $(selection).attr("placeholder") ? "field error" : "field");
				});
			}
			if ($(element).find(".data").find(".error").size() == 0) {
				if (status == false) {
					status = true;
					if (html) {
						$.ajax({
							"url": $(element).find(".data").attr("action"),
							"type": $(element).find(".data").attr("method"),
							"data": $(element).find(".data").serialize(),
							"success": function (data) {								
								if ($(html).size() != 0) {
									$(element).find(".button").replaceWith(html);
									status = false;
								}
							}
						});
					}
					else {
						$(element).find(".data").submit();
					}
				}
			}
			return (false);
		});
		return (this);
	};
})(jQuery);

(function ($) {
	$.fn.engine = function (opts) {
		var element = this;
		var timeout = 0;
		var ratio = 2;
		var diff = (new Date(_DATE).getTime() - new Date().getTime()) / 1000;
		var cvs = $(element).find(".clock").get(0);
		var ctx = cvs.getContext("2d");
		var extraprint = function (index, size, caption, percent) {
			ctx.beginPath();
			ctx.font = (size) + "px Code, Helvetica, Arial";
			ctx.textAlign = "right";
			ctx.fillStyle = _SHADOW;
			ctx.fillText((_CAPTION ? caption : String()), (cvs.width) - (0.05 * cvs.width), (cvs.height) - (size * index - size) - (0.05 * cvs.width));
			ctx.closePath();
			ctx.beginPath();
			ctx.font = (size) + "px Code, Helvetica, Arial";
			ctx.textAlign = "right";
			ctx.fillStyle = _TEXT;
			ctx.fillText((_CAPTION ? caption : String()), (cvs.width) - (0.05 * cvs.width + size / 2 / 10), (cvs.height) - (size * index - size) - (0.05 * cvs.width + size / 2 / 10));
			ctx.closePath();
		};
		var fn = function (d, h, m, s) {
			cvs.style.width = $(element).width();
			cvs.style.height = $(element).height();
			cvs.width = $(element).width() * ratio;
			cvs.height = $(element).height() * ratio;
			ctx.clearRect(0, 0, cvs.width, cvs.height);
			extraprint(4, cvs.width <= cvs.height ? cvs.width / 2 / 4 : cvs.height / 2 / 4, ("00" + d).slice(-3) + " " + (d >= 2 ? "DAYS" : "DAY"), d * (1 / 365));
			extraprint(3, cvs.width <= cvs.height ? cvs.width / 2 / 4 : cvs.height / 2 / 4, ("00" + h).slice(-2) + " " + (h >= 2 ? "HOURS" : "HOUR"), h * (1 / 24));
			extraprint(2, cvs.width <= cvs.height ? cvs.width / 2 / 4 : cvs.height / 2 / 4, ("00" + m).slice(-2) + " " + (m >= 2 ? "MINUTES" : "MINUTE"), m * (1 / 60));
			extraprint(1, cvs.width <= cvs.height ? cvs.width / 2 / 4 : cvs.height / 2 / 4, ("00" + s).slice(-2) + " " + (s >= 2 ? "SECONDS" : "SECOND"), s * (1 / 60));
			timeout = setTimeout(function () {fn(d, h, m, s);}, 1000);
			if (--s < 0 && (s = s + 60)) {
				if (--m < 0 && (m = m + 60)) {
					if (--h < 0 && (h = h + 24)) {
						if (--d < 0 && (d = d + 365)) {
							clearTimeout(timeout);
						}
					}	
				}
			}
		};
		
		fn(parseInt(diff / 86400), parseInt(diff % 86400 / 3600), parseInt(diff % 86400 % 3600 / 60), parseInt(diff % 86400 % 3600 % 60 / 1));
		return (this);
	}
})(jQuery);

(function ($) {
	$.fn.draw = function (opts) {
		var element = this;
		var latitude = _LATITUDE;
		var longitude = _LONGITUDE;
		
		$(window).bind("load resize", function (event) {
			new google.maps.Marker({
				"icon": {
					"url": "resources/images/marker.png",
					"size": new google.maps.Size(64, 64),
					"scaledSize": new google.maps.Size(64, 64)
				},
				"position": new google.maps.LatLng(latitude, longitude),
				"map": new google.maps.Map($(element).find(".map").get(0), {
					"center": new google.maps.LatLng(latitude, longitude),
					"zoom": 10,
					"mapTypeId": google.maps.MapTypeId.ROADMAP,
					"mapTypeControl": false,
					"scrollwheel": false
				})
			});
		});
		return (this);
	};
})(jQuery);

(function ($) {
	$.fn.autoslide = function (opts) {
		var element = this;
		var delay = 5000;
		var current = 0;
		var size = 4;
		var fn = function () {
			$(element).find(".slide").each(function (index) {
				var selection = this;
				
				$(selection).css({"z-index": (index == current ? 1000000 : 0), "opacity": (index == current ? 0.5 : 0)});
			});
			current = current + 1;
			current = current == size ? 0 : current;
			setTimeout(function () {fn();}, delay);
		};

		$(window).bind("load", function () {
			fn();	
		});
		return (this);
	}
})(jQuery);

(function ($) {
	$.fn.interact = function (opts) {
		var element = this;
		var status = true;
		var delay = 1000;
		var count = 0;
		var fn = function () {
			status = status ? false : true;
			$(".interface").css({"margin": (status ? "60px 0 0 0" : 0 - $(".interface").height() + "px 0 0 0"), "opacity": (status ? 1 : 0)});
		};
		
		fn();
		$(element).find(".menu").bind("click", function (event) {
			fn();
			return (false);
		});
		setInterval(function () {
			$(element).find(".menu").css({"opacity": (count ? 0 : 1)});
			count = count ? 0 : 1;
		}, delay / 2);
		return (this);
	};
})(jQuery);

$(function () {
	$(document).spinload();
	$(document).placeholder();
});